package com.dnjn.mes.biz.assist;

import com.dnjn.mes.biz.domain.*;
import com.dnjn.mes.biz.service.*;
import com.dnjn.mes.biz.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.EncodeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;


@Slf4j
@Component("workforceTask")
public class WorkforceTaskHandler {

    @Autowired
    private IMesEquipmentService equipmentService;

    @Autowired
    private IMesWorkforceConfigService workforceConfigService;

    @Autowired
    private IMesWorkforceXrefService xrefService;
    @Autowired
    private IMesWorkforceShiftService workforceShiftService;

    @Autowired
    private IMesWorkforceShiftClockingService clockingService;

    @Autowired
    private WebSocketServer socketServer;

    /**
     * todo quartz job
     */
//    public void handleWorkforceshift(String str) {
    public void handleWorkforceshift(String startTime,String equipmentId) {
        MesEquipment mesEquipment = equipmentService.fetchCurrent();
        if (!mesEquipment.getEquipmentId().equals(Long.parseLong(equipmentId))){
            return;
        }
        List<MesWorkforceConfig> mesWorkforceConfigList = workforceConfigService.lambdaQuery().eq(MesWorkforceConfig::getDeleted, "0").eq(MesWorkforceConfig::getEquipmentId,mesEquipment.getEquipmentId()).list();
        for (MesWorkforceConfig mesWorkforceConfig : mesWorkforceConfigList) {
            String startTime1 = mesWorkforceConfig.getStartTime().toString();
            if (startTime.equals(startTime1)) {
                log.info(mesWorkforceConfig.toString() + "班次在20分钟后要执行了=========================");
                log.info("=========================" + startTime1);
                //将当前班次记录到 wrokforce_shift 班次记录表中 ||
                LocalDate localDate = LocalDateTime.now().toLocalDate();
                MesWorkforceShift mesWorkforceShift = new MesWorkforceShift();
                mesWorkforceShift.setWorkforceId(mesWorkforceConfig.getWorkforceId());
                mesWorkforceShift.setCurrentDate(localDate);
                mesWorkforceShift.setEquipmentId(mesEquipment.getEquipmentId());
                mesWorkforceShift.setWorkforceLabel(mesWorkforceConfig.getWorkforceName());
                mesWorkforceShift.setHasClockin("0");
                mesWorkforceShift.setHasClockout("0");
                workforceShiftService.save(mesWorkforceShift);
                //将当前班次的用户列表记录到用户打卡记录表中 mes_workforce_shift_clocking
                List<MesWorkforceXref> xrefList = xrefService.lambdaQuery().eq(MesWorkforceXref::getWorkforceId, mesWorkforceConfig.getWorkforceId()).eq(MesWorkforceXref::getDeleted,"0").list();
                for (MesWorkforceXref xref : xrefList) {
                    MesWorkforceShiftClocking clocking = new MesWorkforceShiftClocking();
                    clocking.setShiftId(mesWorkforceShift.getShiftId());
                    clocking.setUserId(xref.getUserId());
                    clocking.setUserName(xref.getUserName());
                    clocking.setHasClockin("0");
                    clocking.setHasClockout("0");
                    clocking.setEquipmentId(mesEquipment.getEquipmentId());
                    clockingService.save(clocking);
                }
                    //查询即将打卡班次用户列表返回给前端
                    List<MesWorkforceShiftClocking> clockingList = clockingService.lambdaQuery().eq(MesWorkforceShiftClocking::getShiftId, mesWorkforceShift.getShiftId()).list();
                    for (MesWorkforceShiftClocking clocking : clockingList) {
                        clocking.setPassword("");
                    }
                    mesWorkforceShift.setClockingList(clockingList);
                    //socketServer.onMessage(mesWorkforceShift,"LIST");

            }
        }

    }


   /* public void handleWorkforceshiftEnd(String endTime) throws EncodeException {
        MesEquipment mesEquipment = equipmentService.fetchCurrent();
        List<MesWorkforceConfig> mesWorkforceConfigList = workforceConfigService.lambdaQuery().eq(MesWorkforceConfig::getDeleted, "0").list();
        for (MesWorkforceConfig mesWorkforceConfig : mesWorkforceConfigList) {
            String endTime1 = mesWorkforceConfig.getEndTime().toString();
            if (endTime.equals(endTime1)) {
                LocalDate  currentDate =LocalDateTime.now().minusHours(8).toLocalDate();
                Long workforceId = mesWorkforceConfig.getWorkforceId();
                MesWorkforceShift one = workforceShiftService.lambdaQuery().eq(MesWorkforceShift::getWorkforceId, workforceId).eq(MesWorkforceShift::getCurrentDate, currentDate).one();
                log.info(one.toString() + "班次在20分钟后要执行了=========================");
                socketServer.onMessage(one,"END");
            }
        }
    }*/
}
